<?php
class CARXInput
{
	var $m_dbFieldName; //��� ���� � ���� ������, � ������� ������������ ������ ����� �� ������� ������
	var $m_value; // �������� � ����
	var $m_label; //���������� ����� ����� �����
	var $m_parentForm;
	var $m_comment;
	var $m_readonly;
	var $m_dbConnected;  //whether conntected to DB table or not
	var $m_dbTable;
	var $m_markError;
	var $m_sublabel;


	// $dbFieldName also makes the name of html control
	function CARXInput($label, $dbFieldName = "")
	{
		$this->m_dbFieldName = $dbFieldName;
		$this->m_value = "";
		$this->m_label = $label;
		$this->m_parentForm = NULL;
		$this->m_readonly = FALSE;
		$this->m_dbConnected = TRUE;
		$this->m_dbTable = NULL;
	}



	function SetDBTable(&$table)
	{
		$this->m_dbTable =& $table;
	}

	function GetHTMLCtrlName()
	{
		//$name = "ctrl_";
		$name = $this->m_dbFieldName;
		return $name;
	}

	function SetReadOnly($readonly = TRUE)
	{
		$this->m_readonly = $readonly;
	}

	function GetReadOnly()
	{
		return $this->m_readonly;
	}




	function GetLabel()
	{
		$label = $this->m_label;
		if ($this->m_markError)
		{
			$label = "<font color=\"red\">$label</font>";
		}
		return $label;
	}

	function GetSubLabel()
	{
		if ($this->m_markError)
		{
			return "<br /><font color=\"red\">$this->m_markError</font>";
		}
		if ($this->m_sublabel)
		{
			return '<br />' . $this->m_sublabel;
		}
		return '';
	}



	function UpdateTableData($bCtrlToTable = TRUE)
	{
		if (!$this->IsConnectedToDB()) return;

		if ($bCtrlToTable)
		{
			$table =& $this->GetDBTable();
			$table->SetField($this->m_dbFieldName, $this->m_value);
		}
		else
		{
			$this->m_value = $this->GetValueFromTable();
		}
	}

	/**
         * @return CARXTable
         */
	function &GetDBTable()
	{
		return $this->m_dbTable;
	}


	function SetDBField($fieldName)
	{
		$this->m_dbFieldName = $fieldName;
	}

	function GetDBField()
	{
		return $this->m_dbFieldName;
	}

	function SetValue($value)
	{
		$this->m_value = $value;
	}

	function GetValue()
	{
		return $this->m_value;
	}

	function MarkError($message)
	{
		$this->m_markError = $message;
	}

	function SetSubLabel($message)
	{
		$this->m_sublabel = $message;
	}

	function GetValueFromTable()
	{
		if (!$this->GetDBTable() || !$this->m_dbFieldName) return "";
		$table =& $this->GetDBTable();

		$needToOpen = !$table->m_fields;

		if ($needToOpen)
		{
			if (!is_object($table)) {print_r($table);}
			$table->Open();
		}

		$result = $table->GetField($this->m_dbFieldName);

		if (!is_null(Request('add')) || (Request('edit') == 0))
		{
			if ($value = Request($this->m_dbFieldName))
			{
				$result = $value;
			}
		}

		if ($needToOpen)
		{
			$table->Close();
		}

		return $result;
	}



	function GetFormPostValue()
	{
		$htmlCtrlName = $this->GetHTMLCtrlName();
		$page =& ArxGetPage();
		return $page->Request($htmlCtrlName);
	}

	function Submit()
	{
		if ($this->m_dbFieldName && $this->m_parentForm && $this->m_parentForm->InSubmitMode())
		{
			$this->m_value = $this->GetFormPostValue();
			if ($this->GetDBTable())
			{
				$table =& $this->GetDBTable();
				$this->UpdateTableData(TRUE);
			}
		}

	}
}

?>

  